import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:robotcontrol/entity/common_item.dart';
import 'package:robotcontrol/utils/cache_image.dart';
import 'package:robotcontrol/utils/date_util.dart';
import 'package:robotcontrol/utils/navigator_util.dart';
import 'package:robotcontrol/utils/tip_util.dart';

class FollowItemWidget extends StatefulWidget{
  final Item item;
  const FollowItemWidget({Key key,this.item}):super(key: key);
  @override
  _FollowItemWidget createState() => _FollowItemWidget();
}
class _FollowItemWidget extends State<FollowItemWidget> with AutomaticKeepAliveClientMixin{
  
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      child: Column(
        children: [
          //视频作者介绍
          _videoAuthor(),
          //作者作品
          Container(
            height: 230,
            child: ListView.builder(itemCount:widget.item.data.itemList.length,
                //水平滑动
                scrollDirection: Axis.horizontal,
                itemBuilder: (context,index){
              //单个视频介绍
                  return _inkWell(
                    item: widget.item.data.itemList[index],
                    last: index == widget.item.data.itemList.length - 1
                  );
                }),
          ),
        ],
      ),
    );
  }
  //视频作者介绍
  Widget _videoAuthor(){
    return Container(
      padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
      child: Row(
        children: [
          ClipOval(//圆形头像
            child: cacheImage(widget.item.data.header.icon,width: 40,height: 40),
          ),
          Expanded(flex:1,
              child: Container(
                padding: EdgeInsets.only(left: 10,right: 10),
                child: Column(
                  // 交叉轴对齐设置：即水平方向的对齐设置
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(widget.item.data.header.title,
                    style: TextStyle(color: Colors.black,fontSize: 14),
                    ),
                    Padding(padding: EdgeInsets.only(top: 3),
                    child: Text(
                      widget.item.data.header.description,
                      style: TextStyle(
                        color: Colors.black26,fontSize: 12,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),)
                  ],
                ),
              ),),
          // 剪切圆角矩形
          ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: Container(
              padding: EdgeInsets.all(5),
              decoration: BoxDecoration(color: Color(0xFFF4F4F4)),
              child: Text(
                TipUtil.add_follow,
                style: TextStyle(fontSize: 12,color: Colors.grey),
              ),
            ),
          ),
        ],
      ),
    );
  }
  //单个视频介绍
 Widget _inkWell({Item item,bool last}){
    return InkWell(
      onTap: ()=>{
        toNamed('/videoDetail',item.data)
      },
      child: Container(
        padding: EdgeInsets.only(left: 15,right: last? 15:0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //视频图片
            _videoImage(item),
            //视频名称
            _videoName(item),
            //视频时间
            _videoTime(item)
          ],
        ),
    ),
    );
 }
  //视频图片
 Widget _videoImage(Item item){
   return Stack(
     children: [
       ClipRRect(
         borderRadius: BorderRadius.circular(4),
         child: Hero(
           tag: '${item.data.id}${item.data.time}',
           child: cacheImage(item.data.cover.feed,width: 300,height: 180),
         ),
       ),
       //类型
       Positioned(
         right: 8,
         height: 8,
         child: ClipRRect(
           borderRadius: BorderRadius.circular(5),
           child: Container(
             padding: EdgeInsets.all(6),
             decoration: BoxDecoration(color: Colors.white54),
             alignment: AlignmentDirectional.center,
             child: Text(
               //item.data.category == null?:item.data.category,
   "未知",
               style: TextStyle(
                 fontSize: 13,
                 color: Colors.black,
                 fontWeight: FontWeight.bold
               ),
             ),
           ),
         ),
       )
     ],
   );
  }
  //视频名称
 Widget _videoName(Item item){
    return Container(
      width: 300,
        padding: EdgeInsets.only(top: 3,bottom: 3),
      child: Text(
        item.data.title,
        style: TextStyle(
          fontSize: 14,
          color: Colors.black,
          fontWeight: FontWeight.bold
        ),
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
      ),
    );
  }
  //视频时间
 Widget _videoTime(Item item){
    return Container(
      child: Text(
        formatDateMsByYMDHM(item.data.author.latestReleaseTime),
        style: TextStyle(fontSize: 12,color: Colors.black26),
      ),
    );
  }
}